package com.kandinfo.base.admin.repository;

import java.util.List;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.jpa.repository.Query;

import com.kandinfo.base.admin.constant.CacheConstant;
import com.kandinfo.base.admin.domain.Area;
import com.kandinfo.base.common.repository.BaseRepository;


/**
 * Class Name: AreaRepository Description: 地区
 * 
 * @author wandongwang
 *
 */
public interface AreaRepository extends BaseRepository<Area, Long> {

    /**
     * 
     * Description: search area
     *
     * @return
     */
    @Query(value = "SELECT * FROM area", nativeQuery = true)
    List<Area> searchArea();

    @Query(value = "SELECT * FROM  area WHERE tree_path = ',' order by orders;", nativeQuery = true)
    List<Area> findAllProvince();

    @Query(value = "SELECT * FROM area a WHERE a.tree_path REGEXP '^\\,[0-9]+\\,$'", nativeQuery = true)
    List<Area> findAllCity();

    @Query(value = "select * from area a where a.parent = ?1 order by orders", nativeQuery = true)
    List<Area> findByParent(Long areaId);

    @Query(value = "SELECT a FROM Area a WHERE a.id=?1")
    @Cacheable(value = CacheConstant.AREA_CACHE)
    Area findOne(Long id);

    @Query(value = "SELECT * FROM area where status = 1 order by `orders`", nativeQuery = true)
    List<Area> getStoreArea();
}
